﻿using System;
using System.IO;
using System.Windows.Controls;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using IssueVision.Common;

namespace IssueVision.Client
{
    public partial class IssueEditor : UserControl, ICleanup
    {
        #region "Private Data Members"
        private Lazy<ViewModelBase> _viewModelExport;
        #endregion "Private Data Members"

        #region "Constructor"
        public IssueEditor()
        {
            InitializeComponent();

            // register for ReadOnlyIssueMessage
            AppMessages.ReadOnlyIssueMessage.Register(this, OnReadOnlyIssueMessage);
            // register for OpenFileMessage
            AppMessages.OpenFileMessage.Register(this, OnOpenFileMessage);
            // register for SaveFileMessage
            AppMessages.SaveFileMessage.Register(this, OnSaveFileMessage);

            if (!ViewModelBase.IsInDesignModeStatic)
            {
                // Use MEF To load the View Model
                _viewModelExport = App.Container.GetExport<ViewModelBase>(
                    ViewModelTypes.IssueEditorViewModel);
                if (_viewModelExport != null) DataContext = _viewModelExport.Value;
            }
        }
        #endregion "Constructor"

        #region "ReadOnlyIssueMessage"

        private void OnReadOnlyIssueMessage(Boolean readOnly)
        {
            if (readOnly)
            {
                // prepare the screen for read-only issue
                textBox_IssueTitle.IsReadOnly = true;
                comboBox_Type.IsEnabled = false;
                comboBox_Priority.IsEnabled = false;
                comboBox_Severity.IsEnabled = false;
                comboBox_Status.IsEnabled = false;
                comboBox_Substatus.IsEnabled = false;
                dataGrid_Platform.IsEnabled = false;
                comboBox_AssignedTo.IsEnabled = false;
                comboBox_Resolution.IsEnabled = false;
                textBox_Description.IsReadOnly = true;
                textBox_ReproSteps.IsReadOnly = true;
                listBox_Files.IsEnabled = false;
                button_Add.IsEnabled = false;
                button_Remove.IsEnabled = false;
                button_Save.IsEnabled = false;
                dataGrid_Attributes.IsEnabled = false;
                button_AddAttribute.IsEnabled = false;
                button_RemoveAttribute.IsEnabled = false;
            }
            else
            {
                // prepare the screen for read/update issue
                textBox_IssueTitle.IsReadOnly = false;
                comboBox_Type.IsEnabled = true;
                comboBox_Priority.IsEnabled = true;
                comboBox_Severity.IsEnabled = true;
                comboBox_Status.IsEnabled = true;
                comboBox_Substatus.IsEnabled = true;
                dataGrid_Platform.IsEnabled = true;
                comboBox_AssignedTo.IsEnabled = true;
                comboBox_Resolution.IsEnabled = true;
                textBox_Description.IsReadOnly = false;
                textBox_ReproSteps.IsReadOnly = false;
                listBox_Files.IsEnabled = true;
                button_Add.IsEnabled = true;
                button_Remove.IsEnabled = true;
                button_Save.IsEnabled = true;
                dataGrid_Attributes.IsEnabled = true;
                button_AddAttribute.IsEnabled = true;
                button_RemoveAttribute.IsEnabled = true;
            }
        }

        #endregion "ReadOnlyIssueMessage"

        #region "OpenFileMessage"

        private void OnOpenFileMessage(NotificationMessageAction<FileInfo> message)
        {
            try
            {
                OpenFileDialog ofDialog = new OpenFileDialog();
                ofDialog.Filter = "All Files (*.*)|*.*";
                ofDialog.Multiselect = false;

                // show the dialog box
                if (ofDialog.ShowDialog().GetValueOrDefault(false))
                {
                    // send the file selected back
                    message.Execute(ofDialog.File);
                }

            }
            catch (Exception ex)
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(ex);
            }
        }

        #endregion "OpenFileMessage"

        #region "SaveFileMessage"

        private void OnSaveFileMessage(EntityModel.File saveFile)
        {
            try
            {
                SaveFileDialog sfDialog = new SaveFileDialog();
                // set Filter
                sfDialog.Filter = "All Files (*.*)|*.*";
                // set DefaultExt
                sfDialog.DefaultExt = saveFile.FileName.Contains(".") ? saveFile.FileName.Substring(saveFile.FileName.LastIndexOf('.')) : saveFile.FileName;

                // show the dialog box
                if (sfDialog.ShowDialog().GetValueOrDefault(false))
                {
                    using (Stream stream = sfDialog.OpenFile())
                    {
                        stream.Write(saveFile.Data, 0, saveFile.Data.Length);
                    }
                }
            }
            catch (Exception ex)
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(ex);
            }
        }

        #endregion "SaveFileMessage"

        #region "ICleanup interface implementation"

        public void Cleanup()
        {
            // call Cleanup on its ViewModel
            ((ICleanup)DataContext).Cleanup();
            // cleanup itself
            Messenger.Default.Unregister(this);
            // set DataContext to null and call ReleaseExport()
            DataContext = null;
            App.Container.ReleaseExport(_viewModelExport);
            _viewModelExport = null;
        }

        #endregion "ICleanup interface implementation"
    }
}
